package com.pds.web.testOrganizationInfo.controller;

import com.pds.common.constant.Constants;
import com.pds.common.core.controller.BaseController;
import com.pds.common.core.domain.R;
import com.pds.common.core.page.TableDataInfo;
import com.pds.common.utils.poi.ExcelUtil;
import com.pds.utils.ExcelTemplateUtil;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationInfo;
import com.pds.web.testOrganizationInfo.service.IPdsTestOrganizationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/testOrganizationInfo")
public class PdsTestOrganizationInfoController extends BaseController {

    @Autowired
    private IPdsTestOrganizationInfoService pdsTestOrganizationInfoService;

    /**
     * 新增
     *
     * @param pdsTestOrganizationInfo
     * @return
     */
//    @Log(title = "机构信息新增基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<String> addInfo(@RequestBody PdsTestOrganizationInfo pdsTestOrganizationInfo) {
        Boolean result = pdsTestOrganizationInfoService.addInfo(pdsTestOrganizationInfo);
        if (result) {
            return R.ok("机构注册成功，账号创建成功！");
        } else {
            return R.fail("账号已存在，机构注册失败！");
        }
    }

    /**
     * 删除 -- 根据id
     *
     * @param id
     */
//    @Log(title = "机构信息删除基本信息", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    public R<Boolean> deleteInfo(@RequestParam Long id) {
        return R.ok(pdsTestOrganizationInfoService.deleteInfo(id));
    }

    /**
     * 更新
     *
     * @param pdsTestOrganizationInfo
     * @return
     */
//    @Log(title = "机构信息更新基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R<Boolean> updateInfo(@RequestBody PdsTestOrganizationInfo pdsTestOrganizationInfo) throws Exception {
        return R.ok(pdsTestOrganizationInfoService.updateInfo(pdsTestOrganizationInfo));
    }

    /**
     * 启用、禁用机构
     * @param pdsTestOrganizationInfo
     * @return
     * @throws Exception
     */
    @PostMapping("/updateStatus")
    public R<Boolean> updateStatus(@RequestBody PdsTestOrganizationInfo pdsTestOrganizationInfo) throws Exception {
        pdsTestOrganizationInfoService.updateStatus(pdsTestOrganizationInfo.getId(), pdsTestOrganizationInfo.getStatus());
        return R.ok();
    }

    /**
     * 查询 -- 分页/条件：userId，status
     *
     * @param pdsTestOrganizationInfo
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo listInfo(PdsTestOrganizationInfo pdsTestOrganizationInfo) {
        startPage();
        List<PdsTestOrganizationInfo> list = pdsTestOrganizationInfoService.selectInfoList(pdsTestOrganizationInfo);

        return getDataTable(list);
    }

    /**
     * 保存机构检测范围
     * @param map
     *          - organizationId 机构id
     *          - categoryIds 检测范围科目id
     * @return
     */
    @PostMapping("/addOrganizationCategory")
    public R<Boolean> addOrganizationCategory(@RequestBody Map map) {
        pdsTestOrganizationInfoService.addOrganizationCategory(map);
        return R.ok();
    }

    /**
     * 查询机构检测范围
     * organizationId 机构id
     * @return
     */
    @GetMapping("/getOrganizationCategory")
    public R<List> getOrganizationCategory(String organizationId) {
        List<String> result = pdsTestOrganizationInfoService.getOrganizationCategory(organizationId);
        return R.ok(result);
    }

    /**
     * 检测机构批量导入模板下载
     * @param response
     */
    @GetMapping("downloadTemplate")
    public void downloadTemplate(HttpServletResponse response){
        ExcelTemplateUtil.downloadTemplate(response,"testInfoTemplate.xlsx","检测机构导入模板");
    }

    /**
     * 环保局批量导入运维机构
     */
    @PostMapping("batchImport")
    public R batchImport(MultipartFile file) throws Exception {
        //获取文件名+后缀
        String filename = file.getOriginalFilename();
        if (filename != null) {
            //获取其后缀
            String extension = filename.substring(filename.lastIndexOf(".") + 1);
            if (!(extension.equals("xls") || extension.equals("xlsx"))) {
                //此处为自定义异常捕获,可使用其他方式
                throw new RuntimeException("文件格式有误,请检查上传文件格式");
            }
        }
        //获取文件的输入流
        ExcelUtil excelUtil = new ExcelUtil(PdsTestOrganizationInfo.class);
        //校验表头，防止模板被改
        String topRow = excelUtil.getTopRow(file.getInputStream());
        if(!Constants.TEST_INFO_HEADERS.equals(topRow)){
            throw new Exception(file.getOriginalFilename()+"文件表头被更改，请用正确的模板上传!");
        }
        //读取数据
        List<PdsTestOrganizationInfo> list = excelUtil.importExcel(file.getInputStream());
        if(list==null && list.size()<1){
            throw new Exception(file.getOriginalFilename()+"没有读到需要导入的数据，请检查！");
        }
        pdsTestOrganizationInfoService.batchInsert(list);
        return R.ok();
    }

}
